/**
 
 *声音管理器
 
 *@author 古卧猫王
 
 */

package com.common.musicplayer
{
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundMixer;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.utils.Dictionary;
	
	
	
	public class SoundManager extends EventDispatcher
	{
		private var soundDic:Dictionary;//声音字典
		private var soundChannelDic:Dictionary;//声音通道字典
		private var isMute:Boolean = false;//是否静音
		
		public function SoundManager()
		{
			super();
			init();
		}
		
		private function init():void{
			soundDic = new Dictionary();
			soundChannelDic = new Dictionary();
		}
		
		/**
		 *添加声音 
		 * @param id 声音id
		 * @param url 声音地址
		 * 
		 */                
		public function addSound(id:String,url:String):void{
			
			if(soundDic[id]){
				return;
			}
			
			var sound:Sound = new Sound();
			sound.addEventListener(Event.COMPLETE,addComplete);
			sound.load(new URLRequest(url));
			function addComplete(e:Event):void{
				sound.removeEventListener(Event.COMPLETE,addComplete);
				soundDic[id] = sound;
			}
		}
		/**
		 *添加并播放声音 
		 * @param id 声音id
		 * @param url 声音地址
		 * @param loops 播放次数
		 * 
		 */                
		public function addAndPlaySound(id:String,url:String,loops:int = 0):void{
			if(soundDic[id]){
				play(id,loops);
				return;
			}
			var sound:Sound = new Sound();
			sound.addEventListener(Event.COMPLETE,addAndPlayComplete);
			sound.load(new URLRequest(url));
			function addAndPlayComplete(e:Event):void{
				sound.removeEventListener(Event.COMPLETE,addAndPlayComplete);
				soundDic[id] = sound;
				play(id,loops);
			}
		}
		
		/**
		 *播放指定声音 
		 * @param id 声音id
		 * @param loops 播放次数
		 * 
		 */                
		public function play(id:String,loops:int = 0):void{
			if(isMute){
				return;
			}
			if(soundDic[id]){
				if(soundChannelDic[id]){
					(soundChannelDic[id] as SoundChannel).stop();
					soundChannelDic[id] = (soundDic[id] as Sound).play(0,loops);
				}else{
					soundChannelDic[id] = (soundDic[id] as Sound).play(0,loops);
				}
			}
		}
		/**
		 *停止指定声音 
		 * @param id 声音id
		 * 
		 */                
		public function stop(id:String):void{
			if(soundChannelDic[id]){
				(soundChannelDic[id] as SoundChannel).stop();
			}
		}
		/**
		 *获取指定声音 
		 * @param id 声音id
		 * @return 
		 */                
		public function getSound(id:String):Sound{
			if(soundDic[id]){
				return (soundDic[id] as Sound);
			}
			return null;
		}
		
		/**
		 *获取指定声音通道 
		 * @param id 声音id
		 * @return 
		 */                
		public function getSoundChannel(id:String):SoundChannel{
			if(soundChannelDic[id]){
				return (soundChannelDic[id] as SoundChannel);
			}
			return null;
		}
		
		/**
		 *移除指定声音 
		 * @param id 声音id
		 * 
		 */                
		public function removeSound(id:String):void{
			if(soundDic[id]){
				if(soundChannelDic[id]){
					(soundChannelDic[id] as SoundChannel).stop();
					soundChannelDic[id] = null;
					delete soundChannelDic[id];
				}
				soundDic[id] = null;
				delete soundDic[id];
			}
		}
		
		/**
		 *设置音量 
		 * @param value 音量值（0~1）
		 * 
		 */                
		public function setVolume(value:Number):void{
			SoundMixer.soundTransform = new SoundTransform(value);
		}
		
		/**
		 *停止所有声音 
		 */                
		public function stopAllSound():void{
			SoundMixer.stopAll();
		}
		
		/**
		 *设置静音 
		 * @param b 静音与否
		 */                
		public function set mute(b:Boolean):void{
			isMute = b;
			if(b){
				stopAllSound();
			}
		}
	}
}